package com.example.schoolenterprisealliance.activity;
//最新动态
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.schoolenterprisealliance.MainActivity;
import com.example.schoolenterprisealliance.R;
import com.example.schoolenterprisealliance.adapter.NavigationAdapter;
import com.example.schoolenterprisealliance.adapter.New_InformationAdapter;
import com.example.schoolenterprisealliance.bean.Navigation;
import com.example.schoolenterprisealliance.bean.NewIfomation;
import com.example.schoolenterprisealliance.dialog.CancelDialog;
import com.example.schoolenterprisealliance.dialog.MyDialog;
import com.example.schoolenterprisealliance.io.AppConfig;
import com.example.schoolenterprisealliance.method.Skip;
import com.example.schoolenterprisealliance.service.DynamicService;
import com.example.schoolenterprisealliance.time.Time;

import org.xmlpull.v1.XmlPullParser;

import java.util.ArrayList;
import java.util.List;

public class DynamicActivity extends AppCompatActivity {
    private New_InformationAdapter new_informationAdapter;
    private RecyclerView recyclerView;
    private RecyclerView rv_navigation;
    private NavigationAdapter navigationAdapter;
    private Skip skip;//跳转
    private Intent intent;
    private EditText et_search;
    private ImageView iv_search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic);
        recyclerView=findViewById(R.id.rv_new_information);
        rv_navigation=findViewById(R.id.rv_navigation);
        et_search = findViewById(R.id.et_search);
        iv_search = findViewById(R.id.iv_search);
        //一个是前一个activity的退出，另一个activity的进入。
        overridePendingTransition(R.animator.into_right, R.animator.out_left);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        View view = LayoutInflater.from(DynamicActivity.this).inflate(R.layout.item_new_information, null);
        //添加Android自带的分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(DynamicActivity.this,DividerItemDecoration.VERTICAL));

        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);

        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
        linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv_navigation.setLayoutManager(linearLayoutManager1);
        navigationAdapter=new NavigationAdapter(R.layout.item_navigation, addNavigation());
        rv_navigation.setAdapter(navigationAdapter);
        //设置适配器
        informationData();
        navigationListener();
        searchIntent();
        ivImageIntent();
    }
    //获取最新资讯数据
    private void informationData() {
        //1, 声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        //2, 设置公共的地址
        builder.baseUrl(AppConfig.BASE_URL);
        //3, 设置解析gson
        builder.addConverterFactory(GsonConverterFactory.create());
        //4, 得到Retrofit 对象
        Retrofit retrofit = builder.build();
        //5, 得到接口
        DynamicService dynamicService = retrofit.create(DynamicService.class);
        //6, 得到Call 的方法
        Call<NewIfomation> call = dynamicService.getInformationData();
        //7, 传入接口回调 , 获取数据
        call.enqueue(new Callback<NewIfomation>() {
            @Override
            public void onResponse(Call<NewIfomation> call, Response<NewIfomation> response) {
                //获得数据
                NewIfomation newIfomation = response.body();
                List<NewIfomation.DataBean> data=newIfomation.getData();
                new_informationAdapter =new New_InformationAdapter(R.layout.item_new_information,data,DynamicActivity.this);
                recyclerView.setAdapter(new_informationAdapter);
                //设置跳转监听
                new_informationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent =new Intent(DynamicActivity.this, ItemActivity.class);
                        String title=data.get(position).getTitle();
                        String time= Time.longToString(data.get(position).getTime());
                        String context=data.get(position).getContext();
                        intent.putExtra("title",title);
                        intent.putExtra("time",time);
                        intent.putExtra("context",context);
                        intent.putExtra("layout","dynamic");
                        startActivity(intent);
                        Log.e("position",position+"");
                    }
                });
                //对话框点击确认时根据传回的position移除数据
                new_informationAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                        switch (view.getId())
                        {
                            case R.id.iv_cancel:
                                Log.e("cancel","cancel图片被点击了");
                                CancelDialog cancelDialog=new CancelDialog(DynamicActivity.this, R.layout.content_cancel_dialog, position,new CancelDialog.PriorityListener() {
                                    @Override
                                    public void setActivityText(int position, String string) {
                                        if (string.equals("确认"))
                                            data.remove(position);
                                        Log.e("iv_cancel","CancelDialog点击确认了");
                                        adapter.setNewData(data);
                                    }
                                });
                                Window window= cancelDialog.getWindow();
                                cancelDialog.getWindow().setLayout(350,300);
                                cancelDialog.show();
                                cancelDialog.setCancelable(false);
                                break;
                        }
                    }
                });
                Log.e("responseDy", newIfomation.getClass().toString());
            }

            @Override
            public void onFailure(Call<NewIfomation> call, Throwable t) {
                Log.e("111error", t.getMessage());
                t.printStackTrace();
            }
        });
    }
    //添加导航栏数据
    private List<Navigation> addNavigation() {
        List<Navigation> data = new ArrayList<>();
        Navigation navigation;
        for (int i=0;i<5;i++){
            navigation=new Navigation();
            if (i==0){
                navigation.setId(i);
                navigation.setNavigetion("首页");
                data.add(navigation);
                continue;
            }else if (i==1){
                navigation.setId(i);
                navigation.setNavigetion("联盟介绍");
                data.add(navigation);
                continue;
            }else if (i==2){
                navigation.setId(i);
                navigation.setNavigetion("最新动态");
                data.add(navigation);
                continue;
            }else if (i==3){
                navigation.setId(i);
                navigation.setNavigetion("最新政策");
                data.add(navigation);
                continue;
            }else {
                navigation.setId(i);
                navigation.setNavigetion("互联网+");
                data.add(navigation);
                continue;
            }
        }
        return data;
    }
    //导航栏监听
    private void navigationListener(){
        navigationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TextView tv=view.findViewById(R.id.tv_navigation);
                String s=tv.getText().toString();
                if (s.equals("最新动态")){

                }else {
                    skip=new Skip(DynamicActivity.this,s);
//                    tv.setTextColor(getResources().getColor(R.drawable.text_color_selector));
                    intent=skip.intent();
                    startActivity(intent);
                }

                Log.e("navagationListener",tv.getText().toString());
            }
        });
    }
    //点击搜索框跳转
    private void searchIntent(){
        et_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(DynamicActivity.this, SearchHotActivity.class);
                startActivity(intent);
            }
        });
    }
    // 点击搜索图片跳转
    private void ivImageIntent(){
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(DynamicActivity.this,SearchHotActivity.class);
                startActivity(intent);
            }
        });
    }



}


